package com.jr.controller;

import com.google.gson.Gson;
import com.jr.dao.EnterpriseDao;
import com.jr.dao.impl.EnterpriseDaoImpl;
import com.jr.entry.Enterprise;
import com.jr.pojo.BlackMess;
import com.jr.pojo.InformationInBlackMess;
import com.jr.service.EnterpriseService;
import com.jr.service.SupplierBlackListService;
import com.jr.service.impl.EnterpriseServiceImpl;
import com.jr.service.impl.SupplierBlackListServiceImpl;
import com.jr.util.BlackPageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/black/*")
public class SupplierBlackListServlet extends HttpServlet {
          EnterpriseDao enterpriseDao=new EnterpriseDaoImpl();
       EnterpriseService enterpriseService=new EnterpriseServiceImpl();
       SupplierBlackListService supplierBlackListService = new SupplierBlackListServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        String url=req.getRequestURI();
        String methodName=url.substring(url.lastIndexOf("/")+1);
        Class cla = SupplierBlackListServlet.class;
        Method method= null;
        try {
            method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            method.invoke(cla.newInstance(),req,resp);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
    public  void  delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         String supname = req.getParameter("supname");
         String  account=req.getParameter("account");
        Boolean mess= supplierBlackListService.deleteSupplierBlackList(account,supname);
        BlackMess blackMess=new BlackMess();
        blackMess.setMess(mess);
        Gson gson=new Gson();
        resp.getWriter().println(gson.toJson(blackMess));
    }
    public void  add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String supname= req.getParameter("supname");
        String  reason=req.getParameter("str");
        String account = req.getParameter("account");
        BlackMess blackMess=new BlackMess();
       Boolean boo=supplierBlackListService.insertSupplierBlackList(account,supname,reason);
         BlackPageHelper blackPageHelper=new BlackPageHelper();
        ArrayList<InformationInBlackMess> infos = supplierBlackListService.selectAllMess(account,supname,blackPageHelper);
        blackPageHelper.setList(infos);
        blackPageHelper.setCountData(supplierBlackListService.selectSize(account,supname));
        blackMess.setMess(boo);
        Gson gson=new Gson();
        resp.getWriter().println(gson.toJson(blackMess));
    }
    public void  query(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ename = req.getParameter("str");
        String account = req.getParameter("account");
        String pageindex=req.getParameter("pageindex");
        BlackPageHelper blackPageHelper=new BlackPageHelper();
        Gson gson=new Gson();
        if (pageindex!=null){
            blackPageHelper.setIndexPage(Integer.parseInt(pageindex));
            ArrayList<InformationInBlackMess> infos=supplierBlackListService.selectAllMess(account,ename,blackPageHelper);
            blackPageHelper.setList(infos);
            blackPageHelper.setCountData(supplierBlackListService.selectSize(account,ename));
            resp.getWriter().println(gson.toJson(blackPageHelper));
        }
        else { ArrayList<InformationInBlackMess> infos =supplierBlackListService.selectAllMess(account,ename,blackPageHelper);
            System.out.println(infos.toString());
             blackPageHelper.setList(infos);
             blackPageHelper.setCountData(supplierBlackListService.selectSize(account,ename));
               resp.getWriter().println(gson.toJson(blackPageHelper));
        }

    }
    public  void queryEnterprise(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         String account=req.getParameter("account");
         Enterprise enterprise = enterpriseService.selectEnterpriseByAccount(account);
         Integer id = enterprise.getId();
         ArrayList<Enterprise> infos=enterpriseService.selectAllEnterpriseNotInBlack(id,account);
         Gson gson=new Gson();
         resp.getWriter().println(gson.toJson(infos));

    }
    public void querySocialNo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String str = enterpriseDao.selectEnterpriseByName(name).getSocialUniformCode();
              Gson gson=new Gson();
              resp.getWriter().println(gson.toJson(str));
    }

}
